package com.jt.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class SpringAOP {
    @Pointcut("execution(* com.jt.service.CatServiceImpl.addCat(String))")
    public void pointcut(){

    }

    //前置执行
    @Before("pointcut()")
    public void before(JoinPoint joinPoint){
        Class<?> targetClass = joinPoint.getTarget().getClass();//目标对象类型
        String methodName = joinPoint.getSignature().getName();//方法名
        String className = joinPoint.getSignature().getDeclaringTypeName();//类名
        Object[] args = joinPoint.getArgs();//方法中的参数
        System.out.println("目标对象的参数类型："+targetClass);
        System.out.println("类的名称："+className);
        System.out.println("方法的名称："+methodName);
        System.out.println("参数信息："+ Arrays.toString(args));
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕通知开始");
        long startTime=System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long closeTime=System.currentTimeMillis();
        System.out.println("环绕通知结束");
        long time = closeTime-startTime;
        System.out.println("用时:"+time);
        return result;
    }
}
